/********************************************************/
/*	Copyright (C) 2012 Gong Li Bin			*/
/*	Project:	MapReduce			*/
/*	Author:		gong_libin			*/
/*	Date:		2013_05_20			*/
/*	File:		MrCmdTest.cpp			*/
/********************************************************/

#include "MrAvg.h"
#include "MrCpu.h"
#include "MrDsk.h"
#include "MrIos.h"
#include "MrMem.h"
#include "MrPipe.h"

void MrCmdTestHelp(char* pszProc)
{
	MR_PRINT("Usage: %s [1|2|3|4|5]\n", pszProc);
	MR_PRINT("'1' is get arv information\n");
	MR_PRINT("'2' is get cpu information\n");
	MR_PRINT("'3' is get dsk information\n");
	MR_PRINT("'4' is get ios information\n");
	MR_PRINT("'5' is get mem information\n");

	return;
}

int main(int argc, char* argv[])
{
	CMrAvg CAvg;
	CMrCpu CCpu;
	CMrDsk CDsk;
	CMrIos CIos;
	CMrMem CMem;
	CMrPipe CPipe;
	CMrCmd* pCCmd = NULL;
	char szCmd[MR_VALUE] = { 0 };

	if (argc < 2) {
		MrCmdTestHelp(argv[0]);
		return MR_SUCCESS;
	}

	switch (atoi(argv[1])) {
		case 1:
			pCCmd = &CAvg;
			strcpy(szCmd, MR_AVG_CMD);
			break;
		case 2:
			pCCmd = &CCpu;
			strcpy(szCmd, MR_CPU_CMD);
			break;
		case 3:
			pCCmd = &CDsk;
			strcpy(szCmd, MR_DSK_CMD);
			break;
		case 4:
			pCCmd = &CIos;
			strcpy(szCmd, MR_IOS_CMD);
			break;
		case 5:
			pCCmd = &CMem;
			strcpy(szCmd, MR_MMR_CMD);
			break;
		default:
			break;
	}

	if (NULL != pCCmd) {
		MR_PRINT("Command: %s\n", szCmd);
		if (MR_SUCCESS == CPipe.MrPipeOpen(szCmd, (char*)"r")) {
			if (MR_SUCCESS == CPipe.MrPipeResultMalloc(MR_PIPE)) {
				if (CPipe.MrPipeRead(MR_PIPE) > 0) {
					MR_PRINT("Result:\n%s\n", CPipe.MrPipeResultGet());
					pCCmd->MrCmdInfo(CPipe.MrPipeResultGet());
					pCCmd->MrCmdDisplay();
				}
				else {
					MR_ERROR("Failed to MrPipeRead\n");
				}
				CPipe.MrPipeResultFree();
			}
			else {
				MR_ERROR("Failed to MrPipeResultMalloc\n");
			}
			CPipe.MrPipeClose();
		}
		else {
			MR_ERROR("Failed to MrPipeOpen\n");
		}
	}
	else {
		MrCmdTestHelp(argv[0]);
	}

	return MR_SUCCESS;
}
